package com.marakana;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback { // <1>
	private static final String TAG = "Preview";

	SurfaceHolder mHolder; // <2>
	public Camera camera; // <3>
	public Activity mMainActivity;

	Preview(Context context) {
		super(context);
		// Install a SurfaceHolder.Callback so we get notified when the
		// underlying surface is created and destroyed.
		mHolder = getHolder(); // <4>
		mHolder.addCallback(this); // <5>
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
	}

	// Called once the holder is ready
	public void surfaceCreated(SurfaceHolder holder) { // <7>
		// The Surface has been created, acquire the camera and tell it where
		// to draw.
		camera = Camera.open(); // <8>

		try {
			camera.setPreviewDisplay(holder); // <9>

			camera.setPreviewCallback(new PreviewCallback() { // <10>
				// Called for each frame previewed
				public void onPreviewFrame(byte[] data, Camera camera) { // <11>

					Preview.this.invalidate(); // <12>
					if (camera.getParameters().getPreviewFormat() == ImageFormat.NV21) {// NV21
						// Convert to JPG
						Size previewSize = camera.getParameters()
								.getPreviewSize();
						YuvImage yuvimage = new YuvImage(data,
								ImageFormat.NV21, previewSize.width,
								previewSize.height, null);
						ByteArrayOutputStream baos = new ByteArrayOutputStream();
						yuvimage.compressToJpeg(new Rect(0, 0,
								previewSize.width, previewSize.height), 50,
								baos);
						byte[] jdata = baos.toByteArray();
						if (mMainActivity != null) {
							((CameraActivity) mMainActivity).getBuffer(jdata);
						}
					}
				}
			});

			Log.e("fff",
					String.valueOf(camera.getParameters().getPictureFormat()));
		} catch (IOException e) { // <13>
			e.printStackTrace();
		}
	}

	// Called when the holder is destroyed
	public void surfaceDestroyed(SurfaceHolder holder) { // <14>
		camera.stopPreview();
		camera.release();
		camera = null;
	}

	// Called when holder has changed
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
		camera.startPreview();
		// camera.setPreviewCallback(new PreviewCallback() {
		//
		// @Override
		// public void onPreviewFrame(byte[] data, Camera camera) {
		// if (mMainActivity != null) {
		// ((CameraActivity) mMainActivity).getBuffer(data);
		// }
		// }
		// });
	}

	public void stopCamera() {
		camera.stopPreview();
		camera.release();
		camera = null;
	}

}